<?php

class ComGalleryController {

    public function invoke() {

        $alias = '';
        if (isset($_GET['alias'])) {
            $alias = $_GET['alias'];
        }


        $item = '';
        if (isset($_GET['item'])) {
            $item = $_GET['item'];
        }


        $derectory = dirname(__FILE__);


        if ($item == 'all_album') {
            $categoryList = $this->getImageCategoryList();

            if (count($categoryList) > 0) {
                $xtemplate = new XTemplate($derectory . DS . 'views' . DS . 'all_album.html');


                foreach ($categoryList as $category) {
                    $xtemplate->assign('ID', $category->getID());
                    $xtemplate->assign('IMAGE', $category->getImage());
                    $xtemplate->assign('TITLE', $category->getTitle());
                    $xtemplate->assign('ALIAS', $category->getAlias());
                    $xtemplate->parse('main.item');
                }

                $xtemplate->parse('main');
                $xtemplate->out('main');
            }
        } else {

            $xtemplate = new XTemplate($derectory . DS . 'views' . DS . 'detail.html');

            // danh sách hình ảnh
            $imageList = $this->getImageListByCategoryID($alias);

            // tên album
            $category = $this->getCategoryByID($alias);
            $xtemplate->assign('CATEGORY_TITLE', $category->getTitle());


            if (count($imageList) > 0) {

                for ($i = 0; $i < count($imageList); $i++) {
                    $xtemplate->assign('ID', $imageList[$i]->getID());
                    $xtemplate->assign('IMAGE', $imageList[$i]->getPath());
                    $xtemplate->assign('TITLE', $imageList[$i]->getTitle());
                    $xtemplate->assign('DESCRIPTION', $imageList[$i]->getDescription());

                    $xtemplate->parse('main.item');
                }
            }
            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
    }

    private function getImageCategoryList() {
        $db = new Database();
        try {
            $categoryObj = new Categories();
            $categoryObj->setComponentID(IMAGE_COMPONENT);
            $result = $categoryObj->getListByComponentID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
        }
    }

    private function getCategoryByID($categoryID) {
        $db = new Database();
        try {
            $categoryObj = new Categories();
            $categoryObj->setID($categoryID);
            $result = $categoryObj->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
        }
    }

    private function getImageListByCategoryID($categoryID) {
        $db = new Database();
        try {
            $imageObj = new Images();
            $imageObj->setCategoryID($categoryID);
            $result = $imageObj->getListByCategoryID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
        }
    }

}

?>
